package gamehistory;


import gamehistory.challenge.ChallengeModelAssimilation;
import gamehistory.player.PlayerModel;
import gamehistory.player.PlayerModelAssimilation;
import gamehistory.utils.xml.XMLModel;
import utils.FileUtilities;
import utils.FormatUtilities;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;

import scg.gen.History;
import scg.gen.ParseException;
import scg.gen.PlayerResult;


/**
 * 
 * @author Brent Kersanske
 *
 */
public class HistoryModel extends XMLModel {
	
	private static final String GAME_HISTORY 	= "gameHistory";
	private static final String NAME		 	= "name";
	private static final String WINNER			= "winner";
	private static final String PLAYERS_PATH    = "players";
	private static final String CHALLENGES_PATH = "challenges";
	
	private static final String GROUP  = "Group";
	private static final String VERSUS = "Vs";
		
	private PlayerModelAssimilation players;
	private ChallengeModelAssimilation challenges;
	
	private String playersOutFile;
	private String challengesOutFile;
	
	private String winner;
	private String historyName;
	
	/**
	 * 
	 * @param historyFile
	 * @throws ParseException
	 * @throws FileNotFoundException
	 */
	public HistoryModel(History gameHistory) {		
		super(GAME_HISTORY);
		assimilateHistory(gameHistory);
	}	
	
	/**
	 * 
	 * @param gameHistory
	 */
	private void assimilateHistory(History gameHistory) {
		this.players     = new PlayerModelAssimilation(gameHistory);
		this.challenges  = new ChallengeModelAssimilation(gameHistory, this.players);		
		PlayerResult currentBestPlayer = gameHistory.getResult().getTable().lookup(0);
		for(PlayerResult playerResult : gameHistory.getResult().getTable()) {
			if(playerResult.getAccount() > currentBestPlayer.getAccount()) {
				currentBestPlayer = playerResult;
			}
		}
		this.winner = currentBestPlayer.getName();
		determineHistoryName();
	}
	
	/**
	 * 
	 * @return
	 */
	private void determineHistoryName() {
		if(this.players.getNumberOfPlayers() != 2) {
			this.historyName = GROUP;
		} else {
			Iterator<PlayerModel> playerIterator = players.getPlayerModels().iterator();
			String playerOne = FormatUtilities.removeSpaces(playerIterator.next().getPlayerName());
			String playerTwo = FormatUtilities.removeSpaces(playerIterator.next().getPlayerName());
			this.historyName = playerOne.concat("_" + VERSUS + "_").concat(playerTwo);
		}
	}	

	/**
	 * Writes the game history to an xml file.
	 * @param parsedHistory
	 * @throws IOException
	 */
	public void writeAssimilationsToXMLFiles(String outputDirectory) {
		this.playersOutFile = outputDirectory.concat(this.historyName.concat(this.players.retrieveFileOutName().concat(FileUtilities.XML_FILE_TYPE)));
		this.challengesOutFile = outputDirectory.concat(this.historyName.concat(this.challenges.retrieveFileOutName().concat(FileUtilities.XML_FILE_TYPE)));
		FileUtilities.writeXMLToFile(this.players.getModel(), outputDirectory, this.historyName.concat(this.players.retrieveFileOutName()));
		FileUtilities.writeXMLToFile(this.challenges.getModel(), outputDirectory, this.historyName.concat(this.challenges.retrieveFileOutName()));
	}
	
	/**
	 * 
	 * @return
	 */
	private String getHumanReadableHistoryName() {
		if(this.players.getNumberOfPlayers() != 2) {
			return GROUP;
		} else {
			Iterator<PlayerModel> playerIterator = players.getPlayerModels().iterator();
			return playerIterator.next().getPlayerName().concat(" " + VERSUS + ". ").concat(playerIterator.next().getPlayerName());
		}
	}
	
	public void buildModel() {
		addAttribute(NAME, 		      getHumanReadableHistoryName());
		addAttribute(WINNER,		  this.winner);
		addAttribute(PLAYERS_PATH,    this.playersOutFile);
		addAttribute(CHALLENGES_PATH, this.challengesOutFile);
	}
}
